package 算法.搜索算法;

/**
 * @author Zhu
 * @Description
 * @create 2023-03-09
 */
public class JZ11旋转数组的最小数字 {
    //二分法
    public int minNumberInRotateArray(int [] array) {
        int left = 0;
        int right = array.length - 1;
        while(left < right){
            int mid = (left + right) / 2;
            //最小的数字在mid右边
            if(array[mid] > array[right])
                left = mid + 1;
                //无法判断，一个一个试
            else if(array[mid] == array[right])
                right--;
                //最小数字要么是mid要么在mid左边
            else
                right = mid;
        }
        return array[left];
    }

    //遍历查找
    public int minNumberInRotateArray2(int [] array) {
        //数组一定有元素
        int res = array[0];
        //遍历数组
        for(int i = 1; i < array.length; i++)
            //每次维护最小值
            res = Math.min(res, array[i]);
        return  res;
    }

}
